//
//  MyGradesVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/1/19.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "MyGradesVC.h"
#import "MyGradesCell.h"
#import "MyVM.h"
#import "PlanTypeView.h"
#import "LotteryTypeView.h"
#import "HomePageVM.h"
#import "LabCell.h"
#import "MJRefresh.h"
#import "MyGradesContentCell.h"
@interface MyGradesVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UICollectionView *topCollectionView;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray  *typeArr;
@property (nonatomic ,strong) NSMutableArray  *dataArr;
@property (nonatomic ,strong) NSMutableArray  *playArr;
@property (nonatomic ,strong) PlanTypeView    *typeView;
@property (nonatomic ,strong) LotteryTypeView *lotteryTypeView;
@end

@implementation MyGradesVC{
    NSInteger   selectBType;
    NSInteger   selectMType;
    NSInteger   selectSType;
    NSString *lId;
    NSInteger   page;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"我的成绩";
    lId=@"0";
    [self getTotal];
    [self.tableView reloadData];
}

- (void)getTotal{
    WS(weakSelf);
    [[HomePageVM manage] getLotteryTypeListWithParentId:@"" Success:^(id returnValue) {
        [weakSelf createData];

    } Fail:^(NSString *errorMessage, NSError *error) {
        
        
    }];

    
}

- (void)createData{
    [self.typeArr removeAllObjects];
    LotteryTypeModel *lModel=[BaseVM manage].lotteryArr[selectBType];
    for (LotteryTypeModel *model in [HomePageVM manage].total) {
        if ([model.PId isEqualToString:lModel.Id]) {
            [self.typeArr addObject:model];
        }
    }
    self.typeView.arrData=self.typeArr;
    [self getPlayType];
}

- (void)getPlayType{
    LotteryTypeModel *model=self.typeArr[selectMType];
    WS(weakSelf);
    [[HomePageVM manage] getPlayList:model.lType andSuccess:^(id returnValue) {
        LotteryPlayModel *allModel=[[LotteryPlayModel alloc]init];
        allModel.Id=@"0";
        allModel.LType=model.lType;
        allModel.PlayName=@"全部";
        weakSelf.playArr=returnValue;
        [weakSelf.playArr insertObject:allModel atIndex:0];
        [weakSelf.topCollectionView reloadData];
        [weakSelf.tableView.header beginRefreshing];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

#pragma mark -- 获取我的成绩
- (void)getMyGrades{
    WS(weakSelf);
    LotteryTypeModel *model=self.typeArr[selectMType];
    LotteryPlayModel *pModel=self.playArr[selectSType];
    [[MyVM manage] getMyBetWithLtype:[model.lType integerValue] andPlayName:pModel.PlayName andPage:page andSuccess:^(id returnValue) {
        NSArray *arr=returnValue;
        if (arr.count<[defaultPageSize integerValue]) {
            [weakSelf.tableView.footer noticeNoMoreData];
        }else{
            [weakSelf.tableView.footer endRefreshing];
        }
        [weakSelf.tableView.header endRefreshing];
        page+=1;
        [weakSelf.dataArr addObjectsFromArray:returnValue];
        [weakSelf.tableView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
    }];
}

#pragma mark -- UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    GradesModel *model=self.dataArr[section];
    
    return 1+model.BettingRecord.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    return view;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        MyGradesCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyGradesCell" forIndexPath:indexPath];
        GradesModel *model=self.dataArr[indexPath.section];
        LotteryTypeModel *tModel=self.typeArr[selectMType];
        cell.type=[tModel.lType integerValue];
        cell.model=model;
        return cell;
    }else{
        MyGradesContentCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyGradesContentCell" forIndexPath:indexPath];
        GradesModel *model=self.dataArr[indexPath.section];
        cell.arr=model.LotteryNum.NumArr;
        cell.model=model.BettingRecord[indexPath.row-1];
        if (indexPath.row%2==1) {
            cell.contentView.backgroundColor=DEFAULTBGCOLOR;
        }else{
            cell.contentView.backgroundColor=[UIColor whiteColor];
        }
        return cell;
    }
    
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.playArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
    LotteryPlayModel *model=self.playArr[indexPath.row];
    [cell setBGColorWithTitle:model.PlayName andCornerRadius:10 andIsSelect:indexPath.row==selectSType];
    [cell.labTitle setFont:[UIFont systemFontOfSize:12]];
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    selectSType=indexPath.row;
    [collectionView reloadData];
    [self.tableView.header beginRefreshing];
}


#pragma mark -- 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        WS(weakSelf);
        _tableView=[[UITableView alloc]init];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=[UIColor whiteColor];
        [_tableView registerClass:[MyGradesCell class] forCellReuseIdentifier:@"MyGradesCell"];
        [_tableView registerClass:[MyGradesContentCell class] forCellReuseIdentifier:@"MyGradesContentCell"];
        _tableView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            page=1;
            [weakSelf.dataArr removeAllObjects];
            [weakSelf.tableView reloadData];
            [weakSelf getMyGrades];
        }];
        _tableView.footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getMyGrades];
        }];
        _tableView.rowHeight=UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight=90;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topCollectionView.mas_bottom);
            make.left.right.bottom.equalTo(self.view);
        }];
    }
    return _tableView;
}

- (UICollectionView *)topCollectionView{
    if (_topCollectionView==nil) {
        WS(weakSelf);
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake((SCREEN_WIDTH-60)/4, 20)];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 20.0;
        _topCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _topCollectionView.backgroundColor = [UIColor whiteColor];
        _topCollectionView.delegate = self;
        _topCollectionView.dataSource = self;
        _topCollectionView.showsVerticalScrollIndicator=NO;
        [_topCollectionView registerClass:[LabCell class] forCellWithReuseIdentifier:@"LabCell"];
        [self.view addSubview:_topCollectionView];
        [_topCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.view);
            make.top.equalTo(self.typeView.mas_bottom).with.offset(5);
            make.height.mas_equalTo(@90);
        }];
    }
    return _topCollectionView;
}

- (LotteryTypeView *)lotteryTypeView{
    if (!_lotteryTypeView) {
        WS(weakSelf);
        _lotteryTypeView=[[LotteryTypeView alloc]init];
        _lotteryTypeView.chaneBlock = ^(NSInteger row) {
            selectBType=row;
            selectMType=0;
            weakSelf.typeView.selectRow=0;
            selectSType=0;
            [weakSelf createData];
        };
        [self.view addSubview:_lotteryTypeView];
        [_lotteryTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(weakSelf.view);
            make.height.mas_equalTo(@40);
        }];
    }
    return _lotteryTypeView;
}

- (PlanTypeView *)typeView{
    if (!_typeView) {
        WS(weakSelf);
        _typeView=[[PlanTypeView alloc]init];
        _typeView.backgroundColor=[UIColor whiteColor];
        _typeView.changeOpen = ^(BOOL isOpen ,NSArray *dataArr) {
            if (isOpen) {
                [weakSelf.typeView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(@(ceilf(dataArr.count/4.0)*40+20));
                }];
            }else{
                [weakSelf.typeView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(@60);
                }];
            }
            
        };
        _typeView.changeValue = ^(NSInteger index) {
            selectMType=index;
            selectSType=0;
            [weakSelf getPlayType];
        };
        [self.view addSubview:_typeView];
        [_typeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.view);
            make.top.equalTo(weakSelf.lotteryTypeView.mas_bottom).with.offset(1);
            make.height.mas_equalTo(@60);
        }];
    }
    return _typeView;
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (NSMutableArray *)typeArr{
    if (!_typeArr) {
        _typeArr=[[NSMutableArray alloc]init];
    }
    return _typeArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
